{extend name="base" /}
{block name="title"}后台管理-网络报修管理{/block}
{block name="main"}
<div class="widget-body">
    <table id="users" class="table table-striped table-bordered dataTable">
        <thead>
        <tr>
            <th>标题</th>
            <th>描述详情</th>
            <th>分类</th>
            <th>标签</th>
            <th>价格</th>
            <th>有效期</th>
            <th></th>
        </tr>
        </thead>
        <tbody>
        {volist name="goods"  id="vo"}
        <tr title="{$vo.gTitle}" gid="{$vo.gId}">
            <td>
                {$vo.gTitle}
            </td>
            <td>
                {$vo.gDetail}
            </td>
            <td>
                {$vo.tName}
            </td>

            <td>
                {$vo.tags}
            </td>
            <td>
                {$vo.price}
            </td>
            <td>
                {$vo.vDate}
            </td>
            <td>
                <div class="btn-group">
                    <a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">
                        操作
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu pull-right">
                        <!-- {:url('edit',['id'=>$vo.gId])} -->
                        <li gid="{$vo.gId}"><a href="#"><i class="icon-edit"></i> 修改</a></li>
                        <li><a href="{:url('del',['id'=>$vo.gId])}"><i class="icon-trash"></i> 删除</a></li>
                    </ul>
                </div>

            </td>
        </tr>
        {/volist}
        </tbody>
    </table>
</div> <!-- /widget-body -->
<script src="__STATIC__/assets/js/jquery.min.js"></script>
<script>
    $('.dropdown-menu>li:first-child').on("click",function(){
        var gid = $(this).attr("gid");
        $.ajax({
            type: "post",
            url: "{:url('goods/edit')}",
            success:function (data) {
                let json = JSON.parse(data);
                var goodstype = $('tr[gid="'+gid+'"]>td:nth-of-type(3)').html();
                $('tr[gid="'+gid+'"]>td:nth-of-type(3)').empty();
                $('tr[gid="'+gid+'"]>td:nth-of-type(3)').append('<select name="type" id="type" style="width: 80px;height: 25px;font-size: 12px;"></select>');
                json.forEach(element => {
                    p = `<option value="${element.tId}">${element.tName}</option>`;
                    $('tr[gid="'+gid+'"]>td:nth-of-type(3)>select').append(p);
                });
                $('select[name = "type"]').on("blur",function(){
                    let tid = $(this).val();
                    console.log(goodstype);
                    $.ajax({
                        type: "post",
                        url: "{:url('goods/update')}",
                        data: {
                            "tid":tid,
                            "gid":gid
                        },
                        success: function(data){
                            let json = JSON.parse(data);
                            if(json.msg=='done'){
                            $('tr[gid="'+gid+'"]>td:nth-of-type(3)').empty();
                            $('tr[gid="'+gid+'"]>td:nth-of-type(3)').append(json.tname);
                            }else if(json.msg=='err'){
                            alert('请不要选择与之前相同的分类');
                            $('tr[gid="'+gid+'"]>td:nth-of-type(3)').empty();
                            $('tr[gid="'+gid+'"]>td:nth-of-type(3)').append(json.tname);
                            }
                        },
                        error: function (error) {
                            console.log(error);
                        }
                    })
                })
            },
        })
    });
</script>
{/block}
